﻿@namespace BootstrapBlazor.Components
@typeparam TItem where TItem : class, new()
@typeparam TChild where TChild : class, new()
@using System.Collections
@using FreeSql.Internal.Model

<AdminModal Visible="Item != null" IsBackdropStatic="false" DialogClassName="modal-lg modal-height0" Title="@(Title.IsNull() ? "【分配】" : Title)" OnClose="OnClose">
    @if (Item != null)
    {
        if (TableTd1 != null && TableRow != null)
        {
            <AdminTable2 TItem="TChild" PageSize="PageSize" Context="item2"
                IsAdd="false" IsEdit="false" IsRemove="false" IsSearchText="IsSearchText" IsQueryString="false" IsAutoSelectParent="true"
                OnSelectChanged="AllocChanged" OnQuery="OnQuery">
                <TableTh1>@TableTh1</TableTh1>
                <TableTd1>@TableTd1(item2)</TableTd1>
                <TableHeader>@TableHeader</TableHeader>
                <TableRow>@TableRow?.Invoke(item2)</TableRow>
            </AdminTable2>
        }
        else if (TableTd1 != null)
        {
            @* 只有 Td1 时生成一行四列的 Table *@
            <AdminTable2 TItem="TChild" PageSize="PageSize" Context="item2"
                IsAdd="false" IsEdit="false" IsRemove="false" IsSearchText="IsSearchText" IsQueryString="false" IsAutoSelectParent="true"
                OnSelectChanged="AllocChanged" OnQuery="OnQuery">
                <TableTh1>@TableTh1</TableTh1>
                <TableTd1>@TableTd1(item2)</TableTd1>
            </AdminTable2>
        }
        else if (TableRow != null)
        {
            <AdminTable2 TItem="TChild" PageSize="PageSize" Context="item2"
                IsAdd="false" IsEdit="false" IsRemove="false" IsSearchText="IsSearchText" IsQueryString="false" IsAutoSelectParent="true"
                OnSelectChanged="AllocChanged" OnQuery="OnQuery">
                <TableHeader>@TableHeader</TableHeader>
                <TableRow>@TableRow?.Invoke(item2)</TableRow>
            </AdminTable2>
        }

        var selectedCount = allItems.Values.Where(a => a.Selected).Count();
        <div class="modal-footer">
            @if (selectedCount >= 0)
            {
                <button @onclick="e => { foreach (var item in allItems.Values) item.Selected = false; }" type="button" class="ml-2 btn btn-light"><i class="far fa-square"></i> 重置</button>
                <button @onclick="AllocFinish" type="button" class="ml-2 btn btn-success"><i class="fas fa-plus"></i> 确认选择 @selectedCount 项</button>
            }
            else
            {
                <button type="button" class="ml-2 btn btn-light disabled"><i class="far fa-square"></i> 重置</button>
                <button type="button" class="ml-2 btn btn-success disabled"><i class="fas fa-plus"></i> 确认选择 @selectedCount 项</button>
            }
        </div>
    }
</AdminModal>
